﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace wms.Common
{
    class Utils
    {
        public static int? ParseInt(string value) {
            try
            {
                int result = 0;
                if (int.TryParse(value, out result))
                {
                    return result;
                }
                else
                {
                    return null;
                }
            }
            catch (Exception) {
                return null;
            }
        }

        public static int? ParseInt(object value)
        {
            try
            {
                string str = Parse(value);
                return string.IsNullOrWhiteSpace(str) ? null : ParseInt(str);
            }
            catch (Exception)
            {
                return null;
            }
        }

        public static float? ParseFloat(string value)
        {
            try
            {
                float result = 0;
                if (float.TryParse(value, out result))
                {
                    return result;
                }
                else
                {
                    return null;
                }
            }
            catch (Exception) {
                return null;
            }
        }

        public static float? ParseFloat(object value)
        {
            try
            {
                string str = Parse(value);
                return string.IsNullOrWhiteSpace(str) ? null : ParseFloat(str);
            }
            catch (Exception)
            {
                return null;
            }
        }


        public static DateTime? ParseDateTime(object value)
        {
            try
            {
                string str = Parse(value);
                DateTime result;
                DateTime.TryParse(str, out result);
                //if (result != null && result.ToShortDateString().Equals("0001/1/1")) {
                //    return null;
                //}
                if (str=="")
                {
                    return null;
                }
                return result;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public static string ParseDateTime(object value,string format)
        {
            try
            {
                string str = Parse(value);
                DateTime result;
                DateTime.TryParse(str, out result);
                if (result != null && result.ToShortDateString().Equals("0001/1/1"))
                {
                    return null;
                }
                if (result != null) {
                    return result.ToString(format);
                }
                return null;
            }
            catch (Exception)
            {
                return null;
            }
        }


        public static string Parse(object value)
        {
            string defaultString = "";
            return Parse(value, defaultString);
        }

        public static string Parse(object value,string defaultString)
        {
            try
            {
                if (value == null)
                {
                    return defaultString;
                }
                else
                {
                    return Convert.ToString(value);
                }
            }
            catch (Exception)
            {
                return defaultString;
            }
        }

        public static SqlParameter[] ParseToSqlParameter(Dictionary<string,object> dic) {

            SqlParameter[] array = new SqlParameter[dic.Count];
            int i = 0;
            foreach (var item in dic)
            {
                object value = item.Value;
                if (value == null) {
                    value = DBNull.Value;
                }
                array[i++] = new SqlParameter(item.Key, value);
            }
            return array;
        }

        public static Dictionary<string,object> ParseToDictionary(DataRow row) {
            Dictionary<string,object> dic = new Dictionary<string, object>();
            foreach (DataColumn c in row.Table.Columns) {
                dic.Add(c.ColumnName, row[c.ColumnName]);
            }
            return dic;
        }

        public static string Md5(string str)
        {
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] data = System.Text.Encoding.Default.GetBytes(str);
            byte[] result = md5.ComputeHash(data);
            String strReturn = String.Empty;
            for (int i = 0; i < result.Length; i++)
                strReturn += result[i].ToString("x").PadLeft(2, '0');
            return strReturn;
        }
    }
}
